import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_project/config/common_string_config.dart';
import 'package:package_info_plus/package_info_plus.dart';
import '../../config/app_bar_view.dart';
import '../common/privacy/dialog_common_view.dart';
import '../common/update/update_version_utils.dart';
import '../common/utils/screen_size_utils.dart';
import '../config/theme_color.dart';

class ContactUsPage extends StatefulWidget {
  final String title;

  const ContactUsPage({this.title = '', super.key});

  @override
  State<ContactUsPage> createState() => _ContactUsPageState();
}

class _ContactUsPageState extends State<ContactUsPage> {
  @override
  Widget build(BuildContext context) {
    return CommonAppWidget.navAppBar(
      title: widget.title,
      childWidget: SizedBox(
        width: ScreenSizeUtils.deviceScreenHeight(context),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            const SizedBox(
              height: 50,
            ),
            Image.asset(
              'assets/images/ic_launcher.png',
              width: 80,
            ),
            const SizedBox(
              height: 20,
            ),
            Text(
              CommonStringConfig.appTitle,
              style: const TextStyle(fontSize: 16, fontWeight: FontWeight.w500),
            ),
            FutureBuilder(
                future: getVersionInfo(),
                builder: (context, snap) {
                  return Text(
                    'Ver${CommonStringConfig.appVersion}',
                    style: TextStyle(
                        fontSize: 14, color: ThemeColor.hint777777Color),
                  );
                }),
            const SizedBox(
              height: 20,
            ),
            getItem(
                leftName: '联系客服',
                tapAction: () {
                  ConfirmAlertDialog.showView(context,rightAction: (){
                    Navigator.pop(context);
                  });
                }),
            const Divider(
              height: 1,
              color: Color(0xffe8e8e8),
            ),
            FutureBuilder(
                future: getVersionInfo(),
                builder: (context, snap) {
                  return getItem(
                      leftName: '当前版本',
                      rightName: CommonStringConfig.appVersion,
                      tapAction: () {
                        if (Platform.isAndroid &&
                            UpdateVersionUtils.checkUpdateVersion(context)) {
                          return;
                        }
                      });
                }),
            const Spacer(),
            Text(
              'Copyright@2023 hainanleyou All Rights Reserved',
              style: TextStyle(fontSize: 12, color: ThemeColor.grayA7A7A7Color),
            ),
            const SizedBox(
              height: 20,
            ),
          ],
        ),
      ),
    );
  }

  Widget getItem({
    String leftName = '',
    Function? tapAction,
    String rightName = '',
  }) {
    return GestureDetector(
      onTap: () {
        if (tapAction != null) {
          tapAction();
        }
      },
      child: Container(
        color: Colors.white,
        padding: const EdgeInsets.symmetric(horizontal: 15, vertical: 15),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Text(
              leftName,
              style: const TextStyle(fontSize: 14),
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.end,
              children: [
                Text(
                  rightName,
                  style: TextStyle(
                    fontSize: 14,
                    color: ThemeColor.grayB5B5B5Color,
                  ),
                ),
                Icon(
                  Icons.arrow_forward_ios_rounded,
                  size: 14,
                  color: ThemeColor.grayB5B5B5Color,
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }

  @override
  void initState() {
    super.initState();
  }

  getVersionInfo() async {
    PackageInfo packageInfo = await PackageInfo.fromPlatform();
    CommonStringConfig.appVersion = packageInfo.version;
  }
}
